package com.oscar.ehome.view.setview;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.oscar.ehome.R;
import com.oscar.ehome.base.activity.BasesActivity;
import com.oscar.ehome.bean.pagebean.ClassRoomPrombleBean;
import com.oscar.ehome.presenter.KnowelgePresenter;
import com.oscar.ehome.presenter.SetPresenter;
import com.oscar.ehome.utils.pullrefresh.PullToRefreshBase;
import com.oscar.ehome.utils.pullrefresh.PullToRefreshListView;
import com.oscar.ehome.utils.system.SystemConfigFactory;
import com.oscar.ehome.utils.util.CommonUtils;
import com.oscar.ehome.utils.util.ToastUtil;
import com.oscar.ehome.utils.view.progressview.ProgressFrameLayout;
import com.oscar.ehome.view.konwledge.MyPrombleListActivity;
import com.oscar.ehome.view.loginview.LoginActivity;
import com.oscar.ehome.view.pageview.adapter.ClassRoomPrombleAdapter;
import com.oscar.ehome.view.pageview.view.QuestionListView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by juxing
 * author: MengWanhe
 * date: On 2017/9/21
 */

public class MyQuestionActivity extends BasesActivity<QuestionListView, SetPresenter> implements QuestionListView, PullToRefreshBase.OnRefreshListener,
        ProgressFrameLayout.AgainRequestListener {

    /**
     *
     */
    @BindView(R.id.title_img_back)
    ImageView titleImgBack;
    /**
     *
     */
    @BindView(R.id.title_layout_left)
    RelativeLayout titleLayoutLeft;
    /**
     *
     */
    @BindView(R.id.title_topbar)
    TextView titleTopbar;
    /**
     *
     */
    @BindView(R.id.basetitle_bar)
    RelativeLayout basetitleBar;
    /**
     *
     */
    @BindView(R.id.myquestion_listview)
    PullToRefreshListView myquestionListview;
    /**
     *
     */
    @BindView(R.id.myquestion_fragment)
    ProgressFrameLayout myquestionFragment;

    private int page = 1;//页数
    private int page_size = 20;//每页的条数
    private String token = "";
    private String s = "1";
    List<ClassRoomPrombleBean.PrombleListBean> list = new ArrayList<>();
    SetPresenter presenter;
    ClassRoomPrombleAdapter adapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_question);
        ButterKnife.bind(this);
        initView();
        OnClicks();
        initData();
    }

    @Override
    protected SetPresenter createPresenter() {
        presenter = new SetPresenter(this);
        return presenter;
    }


    public void initView() {
        basetitleBar.setBackgroundColor(Color.parseColor(SystemConfigFactory.getInstance(MyQuestionActivity.this).getSystemConfig().getColor()));
        titleImgBack.setImageResource(R.mipmap.fanhui);
        titleTopbar.setText("我的问题");
        token = SystemConfigFactory.getInstance(MyQuestionActivity.this).getSystemConfig().getToken();

        adapter = new ClassRoomPrombleAdapter(MyQuestionActivity.this, list);
        myquestionListview.setAdapter(adapter);
    }

    /**
     * 判断是否有网
     */
    public boolean isNetWork() {
        boolean netWork = true;
        if (!CommonUtils.isNetConn(MyQuestionActivity.this)) {
            netWork = false;
            return netWork;
        }
        return netWork;
    }

    /**
     * 加载数据
     */
    public void initData() {
        if (isNetWork()) {
            myquestionFragment.stopRefresh();
            myquestionListview.setVisibility(View.VISIBLE);
            presenter.getMyQuestionList(token, s, "20");
            showLoading();
        } else {
            myquestionFragment.setVisibility(View.VISIBLE);
            myquestionFragment.setLoadFail();
            myquestionListview.setVisibility(View.GONE);
        }
    }

    public void OnClicks() {
        myquestionListview.setPullLoadEnabled(true);
        myquestionListview.setOnRefreshListener(this);
        myquestionFragment.setAgainRequestListener(this);
    }

    @OnClick(R.id.title_layout_left)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void getKnowPrombles(ClassRoomPrombleBean result) {
        dismissLoading();
        if (result != null) {
            if (page == 1) {
                list.clear();
            }
            list = result.getList();
            if (list.size() > 0) {
                adapter.setLists(list);
            } else {
                myquestionListview.setVisibility(View.GONE);
                myquestionFragment.setNoResult();
            }
        } else {
            myquestionListview.setVisibility(View.GONE);
            myquestionFragment.setNoResult();
        }
    }

    @Override
    public void erroy() {
        dismissLoading();
        myquestionListview.onPullDownRefreshComplete();
        myquestionListview.onPullUpRefreshComplete();
        ToastUtil.show(MyQuestionActivity.this, getString(R.string.erry));
        myquestionFragment.setLoadFail();
        myquestionListview.setVisibility(View.GONE);
    }

    @Override
    public void erry(String result) {
        dismissLoading();
        myquestionListview.onPullDownRefreshComplete();
        myquestionListview.onPullUpRefreshComplete();
        ToastUtil.show(MyQuestionActivity.this, result);
        if (result.contains("过期")) {
            getSharedPreferences("appbase", Context.MODE_PRIVATE).edit().clear().commit();
            SystemConfigFactory.getInstance(MyQuestionActivity.this).getSystemConfig().SetLogin(false);
            SystemConfigFactory.getInstance(MyQuestionActivity.this).getSystemConfig().setIsFirst(true);
            Intent intent = new Intent(MyQuestionActivity.this, LoginActivity.class);
            startActivity(intent);
            finish();
        } else {
            myquestionFragment.setLoadFail();
            myquestionListview.setVisibility(View.GONE);
        }
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase refreshView) {
        page = 1;
        s = String.valueOf(page);
        initData();
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase refreshView) {
        page++;
        s = String.valueOf(page);
        initData();
    }

    @Override
    public void againRequest() {
        if (isNetWork()) {
            initData();
        } else {
            ToastUtil.show(MyQuestionActivity.this, getString(R.string.pull_to_refresh_network_error));
        }
    }
}
